package com.shangxian.giveithome.service.impl;

import com.shangxian.giveithome.code.AttentionCode;
import com.shangxian.giveithome.code.CommonCode;
import com.shangxian.giveithome.code.UserCode;
import com.shangxian.giveithome.entity.PageableDetails;
import com.shangxian.giveithome.entity.UserDetails;
import com.shangxian.giveithome.entity.community.Attention;
import com.shangxian.giveithome.entity.community.AttentionUserList;
import com.shangxian.giveithome.entity.community.IssueCount;
import com.shangxian.giveithome.entity.community.UserList;
import com.shangxian.giveithome.exception.ExceptionUtils;
import com.shangxian.giveithome.mapper.*;
import com.shangxian.giveithome.service.AttentionService;
import com.shangxian.giveithome.utils.PageUtils;
import com.shangxian.giveithome.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AttentionServiceImpl implements AttentionService {
    @Resource
    private AttentionMapper attentionMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private IssueMapper issueMapper;

    @Autowired
    private IssueImgMapper issueImgMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private CollectionsMapper collectionsMapper;

    @Override
    @Transactional
    public void attentionUser(Attention attention, HttpServletRequest request) {

        UserDetails userDetails = redisUtils.getHeader(request);
        System.out.println(attention);
        if (attention.getAttentionId().equals(userDetails.getId()) ){
            ExceptionUtils.error(CommonCode.PARAMETER_ERROR);
        }
        Long userId = userDetails.getId();
        //判断该用户是否已经关注
        int count = attentionMapper.findAttentionUser(userId, attention.getAttentionId());
        if (count==0){
            attention.setUserId(userId);
            attention.setAttentionTime(new Date());
            System.out.println(attention.toString());
           attentionMapper.attentionUser(attention);
        }
        else {
            ExceptionUtils.error(AttentionCode.UPLOAD_ERROR);
        }

    }

    @Override
    public void cancelAttention(Long attentionId, HttpServletRequest request) {
        UserDetails userDetails = redisUtils.getHeader(request);
        Long userId = userDetails.getId();
        int i = attentionMapper.cancelAttention(userId, attentionId);
        System.out.println(i);
        if (i ==0){
            ExceptionUtils.error(UserCode.USER_DATA_ERROR);
        }
        redisUtils.upUserDetails(request, userDetails);
    }

    @Override
    public PageableDetails findAllAttentionUser(HttpServletRequest request, Pageable pageable) {
        int pageNumber = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        UserDetails userDetails = redisUtils.getHeader(request);
        //查询总条数
        int total = attentionMapper.attentionUserCount(userDetails.getId());
        if (total ==0){
            return new PageableDetails(pageNumber,pageSize,total,returnPages(total,pageSize),new ArrayList<>());
        }
        List<Long> ids = attentionMapper.findAttentionIds(userDetails.getId());
        List<IssueCount> issueCounts = attentionMapper.findIssueCount(ids);
       // pageNumber = pageNumber*pageSize;
        List<AttentionUserList> attentionUser = attentionMapper.findAllAttentionUser(ids,pageNumber, pageSize);
        attentionUser.forEach(attentionUserList -> {
            attentionUserList.setState(1);
                for (IssueCount issueCount : issueCounts) {
                    if (attentionUserList.getId().equals(issueCount.getUserId())) {
                        attentionUserList.setIssueCount(issueCount.getIssueCount());
                        break;
                    }
                }
        });
        return new PageableDetails<>(pageNumber,pageSize,total,returnPages(total,pageSize),attentionUser);
    }

    @Override
    public UserList findAttentionCollectionUserMessage(Long id, HttpServletRequest request) {
        UserList userList = attentionMapper.findAttentionCollectionUserMessage(id);
        if (userList ==null){
            ExceptionUtils.error(CommonCode.DATA_ERRORS);
        }
        if (request !=null) {
            UserDetails userDetails = redisUtils.getHeader(request);
            if (userList.getId().equals(userDetails.getId())) {
                userList.setState(2);
            } else {
                int i = attentionMapper.findAttentionState(userList.getId(), userDetails.getId());
                if (i > 0) {
                    userList.setState(1);
                }
            }
        }
        return userList;
    }


    /**
     * 返回
     *
     * @param pageNumber 当前页
     * @param pageSize   每页条数
     * @param count      总条数
     * @param list       返回数据
     * @param <T>        泛型
     * @return
     */
    public <T> PageableDetails returnPageable(int pageNumber, Integer pageSize, Integer count, List<T> list) {
        return new PageableDetails<>(pageNumber, pageSize, count, list);
    }

    /**
     * 返回总页数
     *
     * @param total
     * @param pageSize
     * @return
     */
    public int returnPages(int total, int pageSize) {
        return PageUtils.returnPages(total, pageSize);
    }


}
